import { NextApiRequest, NextApiResponse } from "next";

import { getApps, getAppListOfCurrentUser } from "@/services";
import { pickData } from "@/utils/base";

async function getAppSimpleList(
  req: NextApiRequest,
  res: NextApiResponse<APIRes>
) {
  const appListOfCurUser = await getAppListOfCurrentUser(req);
  const appList = appListOfCurUser.map((data) => {
    return pickData(data.app, ["id", "name", "introduce", "type"]);
  });

  res.status(200).json({ success: true, data: appList, message: "success" });
}

export default async function handler(
  req: NextApiRequest,
  res: NextApiResponse<APIRes>
) {
  if (req.method === "GET") {
    getAppSimpleList(req, res);
  } else {
    res
      .status(405)
      .json({ success: false, data: {}, message: "Method not allowed" });
  }
}
